console.log("项目入口");

var express = require("express");
var app = express();

// 配置允许跨域
var cors = require("cors");
app.use(cors());

// 注册cookieParser
var cookirParser = require("cookie-parser");
app.use(cookirParser());

var mysql = require("./util/sqlhelper");

// 注册body-parser
var bodyParser = require("body-parser");
app.use(bodyParser.json());

var needle = require("needle");

// 获取新闻列表信息
app.get("/api/news/:page/:channelId", async function (req, res) {
    needle.get(`https://bh3.mihoyo.com/content/bh3Cn/getContentList?pageSize=10&pageNum=${req.params.page}&channelId=${req.params.channelId}`, function (err, result) {
        res.jsonp(result.body);
    })
});

// 获取壁纸列表信息
app.get("/api/papers/:page", async function (req, res) {
    needle.get(`https://bh3.mihoyo.com/content/bh3Cn/getContentList?pageSize=9&pageNum=${req.params.page}&channelId=177`, function (err, result) {
        res.jsonp(result.body);
    })
});

// 登录密码用户验证
app.get("/api/user/:username/:password", function (req, res) {
    // req.query    ?next=/login
    // req.body     ?post请求携带的数据 name="qiku"
    // req.params   /api/roles?:id
    mysql.query(`select * from user where username = "${req.params.username}"`, function (data) {
        if (data[0].password == req.params.password) {
            res.json({
                "error_code": 0,
                "msg": "用户登录成功",
                "datas": data[0]
            })
        }
        else {
            res.json({
                "error_code": 1002,
                "msg": "用户名或者密码错误"
            })
        }
    }, function (err) {
        res.json({
            "error_code": 1002,
            "msg": "用户名或者密码错误"
        })
    })
})

// 注册用户
app.post("/api/user", function (req, res) {
    var username = req.body.username;
    var password = req.body.password;

    mysql.query(`insert into user (username, password) values ("${username}","${password}")`, function (data) {
        res.json({
            "error_code": 0,
            "msg": "注册用户成功",
            "datas": {
                "id": data.insertId,
                "name": username,
                "password": password
            }
        })
    }, function (err) {
        res.json({
            "error_code": 1001,
            "msg": err.sqlMessage
        })
    })
})

// 获取商品列表信息
app.get("/api/goods", function (req, res) {
    // req.query    ?next=/login
    // req.body     ?post请求携带的数据 name="qiku"
    // req.params   /api/roles?:id
    mysql.query(`select * from good`, function (data) {
        res.json({
            "error_code": 0,
            "msg": "用户登录成功",
            "datas": data
        })
    }, function (err) {
        res.json({
            "error_code": 1004,
            "msg": err.sqlMessage
        })
    })
})

// 加入购物车功能
app.post("/api/cart", function (req, res) {
    var goodid = req.body.id;
    var username = req.body.username;

    mysql.query(`select * from user where username = "${username}"`, function (data) {
        var userid = data[0].id;
        console.log(userid);
        mysql.query(`select num from cart where goodid = ${goodid} and userid = ${userid}`, function (data1) {
            if (data1[0] != undefined) {
                var num = data1[0].num;
                mysql.query(`update cart set num = ${num + 1} where goodid = ${goodid}`, function (data2) {
                    res.json({
                        "error_code": 0,
                        "msg": "加入购物车成功",
                        "datas": {
                            "id": data2.insertId,
                            "userid": userid,
                            "goodid": goodid,
                            "num": num + 1
                        }
                    })
                }, function (err3) {
                    res.json({
                        "error_code": 1005,
                        "msg": err3.sqlMessage
                    })
                })
            }
            else {
                mysql.query(`insert into cart (userid, goodid, num) values ("${userid}","${goodid}","1")`, function (data2) {
                    res.json({
                        "error_code": 0,
                        "msg": "加入购物车成功",
                        "datas": {
                            "id": data2.insertId,
                            "userid": userid,
                            "goodid": goodid,
                            "num": 1
                        }
                    })
                }, function (err3) {
                    res.json({
                        "error_code": 1005,
                        "msg": err3.sqlMessage
                    })
                })
            }
        }, function (err2) {
            res.json({
                "error_code": 1006,
                "msg": err2.sqlMessage
            })
        })
    }, function (err1) {
        res.json({
            "error_code": 1002,
            "msg": err1.sqlMessage
        })
    })
})

// 购物车信息获取
app.get("/api/cart/:username", function (req, res) {
    mysql.query(`select * from user where username = "${req.params.username}"`, function (data) {
        var userid = data[0].id;
        mysql.query(`select * from cart where userid = "${userid}"`, function (data1) {
            if (data1[0] == undefined) {
                res.json({
                    "error_code": 1,
                    "msg": "购物车没有商品",
                    "datas": data1[0]
                })
            }
            else {
                res.json({
                    "error_code": 0,
                    "msg": "购物车没有商品",
                    "datas": data1
                })
            }

        }, function (err1) {
            res.json({
                "error_code": 1010,
                "msg": err1.sqlMessage
            })
        })
    }, function (err) {
        res.json({
            "error_code": 1009,
            "msg": "用户不存在"
        })
    })
})

// 获取指定商品信息
app.get("/api/good/:goodid", function (req, res) {
    mysql.query(`select * from good where id = "${req.params.goodid}"`, function (data) {
        res.json({
            "error_code": 0,
            "msg": "用户登录成功",
            "datas": data[0]
        })
    }, function (err) {
        res.json({
            "error_code": 10010,
            "msg": err.sqlMessage
        })
    })
})

// 删除指定商品信息
app.delete("/api/cart/:goodid/:username", function (req, res) {
    mysql.query(`select * from user where username = "${req.params.username}"`, function (data) {
        var userid = data[0].id;
        mysql.query(`delete from cart where goodid = "${req.params.goodid}" and userid = "${userid}"`, function (data1) {
                res.json({
                    "error_code": 0,
                    "msg": "购物车删除商品成功"
                })
        }, function (err1) {
            res.json({
                "error_code": 1010,
                "msg": err1.sqlMessage
            })
        })
    }, function (err) {
        res.json({
            "error_code": 1009,
            "msg": "用户不存在"
        })
    })
})

// 更改项目数量
app.put("/api/cart", function (req, res) {
    mysql.query(`select * from user where username = "${req.body.username}"`, function (data) {
        var userid = data[0].id;
        mysql.query(`update cart set num = "${req.body.num}" where goodid = ${req.body.goodid} and userid = "${userid}"`, function (data) {
            res.json({
                "error_code": 0,
                "msg": "修改角色信息成功"
            })
        }, function (err1) {
            res.json({
                "error_code": 10011,
                "msg": "商品更改信息失败"
            })
        })
    }, function (err) {
        res.json({
            "error_code": 1009,
            "msg": "用户不存在"
        })
    })


})

app.listen(6766, function () {
    console.log("Server Run ...");
})